import moment from "moment";
import * as ddscode from "dingtalk-jsapi";
let app = getApp();

Page({
  data: {
    purchaseOrderList: [],
    slotModalShow: false,
    slotButtonList: [
      {
        name: '关闭',
      }
    ],

    StatusOptions: [{
      value: 0,
      name: '交通'
    },
    {
      value: 1,
      name: '餐饮'
    }],
    statusindex: '',
    startData: '',  //时间相关
    endData: '',

    activeIndex: 0,
    // checked: true,

    page:1,
    page_size:20,
    searchInput:"",
    dowm:true,
    showEndTip: false, // 控制是否显示到底提示
  },

  onLoad() {
    var that = this;
    var date = new Date();
    that.setData({
      purchaseOrderList: [],
    })
    setTimeout(() => {
      this.setData({
        activeIndexDefault: 1
      })
    }, 2000)
    // that.getPurchaseOrderList()

  },

  onShow() {
    var that = this;
    that.setData({
      purchaseOrderList: [],
    })
    that.getPurchaseOrderList()
    
  },

  saomaya() {
    dd.scan({
      type: 'qr',
      success: (res) => {
        // 扫码成功后的回调函数
        dd.alert({ title: res.code });
        const scanResult = res.code; // 获取扫码结果
        const searchBar = this.selectComponent("#searchBar"); // 通过id选择搜索框组件
        searchBar.setData({ value: scanResult }); // 将扫码结果赋值给搜索框的value属性
      },
      fail: (err) => {
        // 扫码失败后的回调函数，可以根据实际需求进行处理
      }
    });
  },

  onWithSwiperTabItemTap(index) {
    console.error("index.====", index);
    this.setData({
      withSwiperTabActiveIndex: index
    })
  },

  //状态下拉框 状态改变可以拿到下标 赋值 还可以根据下标知道选中的是哪个值 
  statusChange(e) {
    this.setData({
      statusindex: e.detail.value,
      status: e.detail.value
    })
  },

  // 获取输入内容进行搜索
  onSearchInput(e){
    console.log("e===>>",e)
    var that = this;
    that.setData({
      searchInput:e,
      page:1,
      dowm:true,
      purchaseOrderList:[]
    })
    
    that.getPurchaseOrderList();

  },
  onSearchClear(e){
    var that = this;
    that.setData({
      searchInput:'',
      page:1,
      dowm:true,
      purchaseOrderList:[]
    })
    that.getPurchaseOrderList();
  },


  // 获取配货单未完成数据
  getPurchaseOrderList(e) {
    var that = this;
    var currentContentId = that.data.currentContentId
    var bx_status = 0;
    if(currentContentId == 0){
      bx_status = 0
    }else if(currentContentId == 1){
      bx_status =1
    }
    var data = JSON.stringify(
      {
        "xx": {
          "token": app.globaldata.user.token
        },
        "status": {
          "status": bx_status
        }
      }
    )

    // var data = JSON.stringify(
    //   {
    //     "q": {
    //       "encrypted_text": "HeaTK4sK5lCeKpEqCVODtA==C/fEgIv6lH2tOB0zs+fqFfHyBXiSGqDrROskYjVb4JIADaaOlLt1Nlghy+wqRYNp",
    //       "key": "65b9b5dde3c7b400",
    //       "sql": "SELECT T1.FID AS 员工id,t1.FNUMBER as 员工编码,t2.FNAME as 员工姓名 FROM T_HR_EMPINFO t1 inner join T_HR_EMPINFO_L t2 ON T1.FID=T2.FID where t1.FDOCUMENTSTATUS='C' and t1.FFORBIDSTATUS ='A' ORDER BY t1.FID",
    //       "fields": [
    //         "员工id","员工编码","员工姓名"
    //       ]
    //     },
    //     "p": {
    //       "page_number": 1,
    //       "page_size": 20
    //     }
    //   }
    // )

    dd.httpRequest({
      url: 'http://openapi.keyboardadmin.com:10003/distribution',
      method: 'POST',
      data: data,
      dataType: 'json',
      success: function (res) {
        console.log('获取配货单未完成数据响应==>', res.data)
        if (res.data.code == 200) {
          var list = res.data.data;
          var purchaseOrderList = that.data.purchaseOrderList;
          for(var i in list){
            purchaseOrderList.push(list[i])
          }
          if(list.length < that.data.page_size){
            that.setData({
              dowm:false
            })
          }
          that.setData({
            purchaseOrderList:purchaseOrderList
          })
        } else {
          console.log("配货未完成的无数据");
          // dd.showToast({
          //   type: 'fail',
          //   content: res.data.data,
          //   duration: 2000,
          // });
        }
      },
      fail: function (res) {
        // dd.alert({ content: 'fail' });
      },
    });
  },




  getIndex(e) {
    console.log("type==>e", e)
    
  },



  onBlur(e) {
    // var that = this
    console.log('onBlur', e.detail)
    // var item = that.data.item
    // item.基本单位数量 =  e.detail
    // this.setData({
    //   item,
    // });
  },


// 点击下推配货单
  gopickingListTwo(e){
    console.log("e==>",e)
    var item = e.currentTarget.dataset.item
    dd.navigateTo({
      url: '/pages/InventoryDetail/InventoryDetail?item='+JSON.stringify(item),
      // url: '/pages/pickingListTwo/pickingListTwo?orderNumber=' + item['FID'] + '&danJubian=' + item['单据编码'] + '&yewuData=' + item['业务日期']

    })
  },


  onReachBottom(){
    var that = this;
    var page = that.data.page;
    var dowm = that.data.dowm;
    var currentContentId = that.data.currentContentId;
    if(dowm){
      that.setData({
        page:page+1
      })
  
      if(currentContentId < 2 ){
        this.getPurchaseOrderList();
      }else{
        this.getReimbursementList();
      }
      
    }
    
  },


});